package com.kly.user.db.dao.impl;

import com.fqgj.common.base.AbstractBaseMapper;
import com.fqgj.common.utils.StringUtils;
import com.kly.user.db.dao.CustomersDao;
import com.kly.user.db.entity.CustomersEntity;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class CustomersDaoImpl extends AbstractBaseMapper<CustomersEntity> implements CustomersDao {

    @Override
    public CustomersEntity getByUserCode(String userCode) {
        Map<String, String> params = new HashMap<>();
        params.put("userCode", userCode);
        return this.getSqlSession().selectOne(this.getStatement("getByUserCode"), params);
    }

    @Override
    public CustomersEntity getCustomer(String mobile, String email, String googleAdId) {
        Map<String, String> params = new HashMap<>();
        params.put("mobile", mobile);
        params.put("email", email);
        params.put("googleAdId", StringUtils.isEmpty(googleAdId) ? "NoData" : googleAdId);
        return this.getSqlSession().selectOne(this.getStatement("getCustomer"), params);
    }

    @Override
    public List<CustomersEntity> listCustomer(String mobile, String email, String googleAdId) {
        Map<String, String> params = new HashMap<>();
        params.put("mobile", mobile);
        params.put("email", email);
        params.put("googleAdId", StringUtils.isEmpty(googleAdId) ? "NoData" : googleAdId);
        return this.getSqlSession().selectList(this.getStatement("listCustomer"), params);
    }

    @Override
    public CustomersEntity addCustomer(String customerCode, String email, String mobile, String adid, Integer trafficType) {
        CustomersEntity entity = new CustomersEntity();
        entity.setCustomerCode(customerCode);
        entity.setEmail(email);
        entity.setMobile(mobile);
        if (StringUtils.isNotEmpty(adid)) {
            entity.setDeviceId(adid);
        }
        entity.setTrafficType(trafficType);
        return this.insert(entity);
    }

    @Override
    public Integer removeByCustomer(String customerCode) {
        return this.getSqlSession().delete(this.getStatement("removeByCustomer"), customerCode);
    }
}